package com.tanhua.server.controller;

import com.tanhua.server.service.VideoMQService;
import com.tanhua.server.service.VideoService;
import com.tanhua.server.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/2/22 14:35
 * @version: 0.0.1
 * @modified By:
 */
@RestController
@RequestMapping("smallVideos")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private MovementsController movementsController;

    @Autowired
    private CommentsController commentsController;

    @Autowired
    private VideoMQService videoMQService;

    /**
     * @param
     * @return
     * @description 保存小视频
     * @author
     * @date
     */
    @PostMapping
    public ResponseEntity<Void> saveVideo(@RequestParam(value = "videoThumbnail", required = false) MultipartFile picFile,
                                          @RequestParam(value = "videoFile", required = false) MultipartFile videoFile) {
        try {
            String videoId= this.videoService.saveVideo(picFile, videoFile);
            if (StringUtils.isNotEmpty(videoId)) {
                this.videoMQService.videoMsg(videoId);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @param
     * @return
     * @description 小视频列表
     * @author
     * @date
     */
    @GetMapping
    public ResponseEntity<PageResult> queryVideoList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            if (page <= 0) {
                page = 1;
            }
            PageResult pageResult = this.videoService.queryVideoList(page, pageSize);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @param
     * @return
     * @description 视频点赞
     * @author
     * @date
     */
    @PostMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String videoId) {
        ResponseEntity<Long> entity = this.movementsController.likeComment(videoId);
        if(entity.getStatusCode().is2xxSuccessful()){
            //发送消息
            this.videoMQService.likeVideoMsg(videoId);
        }
        return entity;
    }

    /**
     * @param
     * @return
     * @description 视频取消点赞
     * @author
     * @date
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String videoId) {
        ResponseEntity<Long> entity = this.movementsController.disLikeComment(videoId);
        if(entity.getStatusCode().is2xxSuccessful()){
            this.videoMQService.disLikeVideoMsg(videoId);
        }
        return entity;
    }

    /**
     * @param
     * @return
     * @description 评论列表
     * @author
     * @date
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity<PageResult> queryCommentsList(@PathVariable("id") String videoId,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return this.commentsController.queryCommentsList(videoId, page, pageSize);
    }

    /**
     * @param
     * @return
     * @description 提交评论
     * @author
     * @date
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity<Void> saveComments(@PathVariable("id") String videoId,
                                             @RequestBody Map<String, String> param) {
        param.put("movementId", videoId);
        ResponseEntity<Void> entity = this.commentsController.saveComments(param);
        if(entity.getStatusCode().is2xxSuccessful()){
            //发送消息
            this.videoMQService.commentVideoMsg(videoId);
        }

        return entity;
    }

    /**
     * @param
     * @return
     * @description 评论点赞
     * @author
     * @date
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity<Long> commentsLikeComment(@PathVariable("id") String videoId) {
        return this.movementsController.likeComment(videoId);
    }
    /**
     * @description 取消评论点赞
     * @author
     * @date
     * @param
     *@return
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity<Long>commentsDisLikeComment(@PathVariable("id")String videoId){
        return this.movementsController.disLikeComment(videoId);
    }

    /**
     * @description 关注用户
     * @author
     * @date
     * @param
     *@return
     */
    @PostMapping("/{id}/userFocus")
    public ResponseEntity<Void>saveUserFocusComments(@PathVariable("id")Long userId){
        try {
            Boolean bool=this.videoService.followUser(userId);
            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @description 取消关注用户
     * @author
     * @date
     * @param
     *@return
     */
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity<Void>saveUserUnFocusComments(@PathVariable("id")Long userId){
        try {
            Boolean bool= this.videoService.disFollowUser(userId);
            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
